import os
import tkinter as tk

import requests
from win10toast import ToastNotifier

# 以下7个变量，需要根据自己的情况进行修改
# 只需要修改下面两个变量即可
# 个人的URL
sign_parameter = '自己登录的URL'
# 用户id(一般来说是唯一的)
userIndex = '自己userIndex'

# 以下变量一般不需要修改
login_IP = 'http://10.23.2.4'  # 登录的URL
not_sign_in_title = 'http://123.123.123.123'  # 未登录跳转页面
result_return = '"result":"success"'  # 返回结果状态
signed_in_title = 'success'  # 登录成功状态
user_ont_online = 'message":"用户已不在线'  # 用户已下线
usr_logout_success = 'message":"下线成功！'  # 用户下线成功
message_box_time = 3  # Windows消息提示时间单位为秒

# 以下4个变量，可根据自己的需要，决定是否修改
already_icon = 'ico/Check.ico'
success_icon = 'ico/Check.ico'
false_icon = 'ico/Cross.ico'
unknown_icon = 'ico/Questionmark.ico'


def window():
    root = tk.Tk()
    root.title('校园网登录')
    root.geometry('300x200')

    canvas = tk.Canvas(root, width=500, height=114)
    image_file = tk.PhotoImage(file='ico/jike.gif')
    image = canvas.create_image(150, 0, anchor='n', image=image_file)
    canvas.pack(side='top')
    tk.Label(root, text='welcome', font=('Arial', 16)).pack()
    btn_login = tk.Button(root, text='登录', command=use_login)
    btn_login.place(x=100, y=150)
    btn_sign_up = tk.Button(root, text='下线', command=use_logout)
    btn_sign_up.place(x=180, y=150)
    root.mainloop()


def use_login():
    try:
        r = requests.get(login_IP, timeout=1)
        # r.encoding ='utf-8'  # 设置编码格式
        req = r.url
    except:
        req = 'False'

    if signed_in_title in req:
        ToastNotifier().show_toast(title="该设备已经登录",
                                   msg="校园网状态",
                                   icon_path=already_icon,
                                   duration=message_box_time,
                                   threaded=True)

    elif not_sign_in_title in req:
        r = requests.get(sign_parameter, timeout=1)
        r.encoding = 'utf-8'
        req = r.text
        if result_return in req:
            ToastNotifier().show_toast(title="登录成功",
                                       msg="校园网状态",
                                       icon_path=success_icon,
                                       duration=message_box_time,
                                       threaded=True)
        else:
            ToastNotifier().show_toast(title="登录失败",
                                       msg="校园网状态",
                                       icon_path=false_icon,
                                       duration=message_box_time,
                                       threaded=True)
    else:
        ToastNotifier().show_toast(title="未连接到校园网",
                                   msg="校园网状态",
                                   icon_path=unknown_icon,
                                   duration=message_box_time,
                                   threaded=True)
    # exits_system()


def use_logout():
    res = requests.post('http://10.23.2.4/eportal/InterFace.do?method=logout', data={
        'userIndex': userIndex})
    res.encoding = 'utf-8'
    if usr_logout_success in res.text:
        ToastNotifier().show_toast(title="退出成功",
                                   msg="校园网状态",
                                   icon_path=success_icon,
                                   duration=message_box_time,
                                   threaded=True)

    elif user_ont_online in res.text:
        ToastNotifier().show_toast(title="用户已下线",
                                   msg="校园网状态",
                                   icon_path=unknown_icon,
                                   duration=message_box_time,
                                   threaded=True)
    else:
        ToastNotifier().show_toast(title="未知异常",
                                   msg="校园网状态",
                                   icon_path=unknown_icon,
                                   duration=message_box_time,
                                   threaded=True)
    # exits_system()


def exits_system():
    os._exit(0)


if __name__ == '__main__':
    window()
